package com.idgenerate.demo.util;

import java.util.concurrent.*;

/**
 * ThreadUtil class
 *
 * @author 蒋时华
 * @date 2018-6-29
 */
public class ThreadUtil {

    public static void ThreadUtil(final int theadNum, final int singleNum, final Runnable task){
        // 锁住所有线程，等待并发执行
        final CountDownLatch begin = new CountDownLatch(1);
        final ExecutorService exec = Executors.newFixedThreadPool(theadNum);
        for (int index = 0; index < theadNum*singleNum; index++){
            exec.submit(task);
        }
        // begin减一，开始并发执行
        begin.countDown();
        //关闭执行
        exec.shutdown();
        // 监听是否已经完成全部线程任务
        while (true){
            if(exec.isTerminated()){
                return;
            }
        }
    }

}
